package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @date: 2021/3/28 0028 20:42
 * @@description: 分类Service
 */
@Service
@Transactional
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;


    public List<Category> findCategoriesByPid(Long pid) {
        //封装查询条件
        Category category = new Category();
        category.setParentId(pid);

        QueryWrapper<Category> queryWrapper = new QueryWrapper(category);

        //执行查询获取结果
        List<Category> categories = categoryMapper.selectList(queryWrapper);

        //处理并返回结果
        if (CollectionUtils.isEmpty(categories)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categories;
    }

    /**
     * 根据分类id集合获取分类集合
     */
    public List<Category> findCategoriesByCids(List<Long> ids) {
        /*//构造查询条件
        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.eq("id",ids);
        //执行查询条件获取结果
        List<Category> categories  = categoryMapper.selectList(queryWrapper);*/

        //selectBatchIds: 根据id批量查询
        List<Category> categories  = categoryMapper.selectBatchIds(ids);

        //处理并返回结果
        if (CollectionUtils.isEmpty(categories)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categories;
    }
}
